package com.example.clouduser.web;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@RestController
public class UserDo1 {

    @RequestMapping("user")
    public String index(HttpServletRequest request){
        final int port = request.getServerPort();
        return "user : " + port;
    }

    @RequestMapping(path = "cookies",
            produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
    public String cookies(HttpServletRequest request){
        final Cookie[] cookies = request.getCookies();
        String html = "";
        if(cookies!=null && cookies.length>0){
            for (Cookie cookie : cookies) {
                html += cookie.getName() + " = " + cookie.getValue() + "<br>";
            }
        } else {
            html = "没有 cookie";
        }
        return html;
    }

}
